﻿using nblTool.utils;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;
using Communication;

namespace _698._45Tool.Utils
{
    internal class TcpHelper
    {
        private static TcpHelper instance = new TcpHelper();
        /// <summary>
        /// 自定义串口消息接收事件
        /// </summary>
        public event EventHandler ReceiveDataEvent;
        private TcpHelper() 
        {
            service.Connecting = (client, e) => {
                Console.WriteLine("有客户端正在连接");
                return EasyTask.CompletedTask;
            };//有客户端正在连接
            service.Connected = (client, e) => {
                Console.WriteLine("客户端成功连接");
                return EasyTask.CompletedTask;
            };//有客户端连接
            service.Disconnected = (client, e) => {
                Console.WriteLine("客户端断开连接");
                return EasyTask.CompletedTask;
            };//有客户端断开连接
            service.Received = (client, e) =>
            {
                ReceiveDataEventArg arg = new ReceiveDataEventArg();
                //从服务器收到信息
                string mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);
                //arg.Data = Convert.to
                arg.Data = System.Text.Encoding.UTF8.GetBytes(mes);
                //Console.WriteLine($"从{client.Id}接收到信息：{mes}");
                
                if (ReceiveDataEvent!=null&& AnalysisMessage.GetValidFrameByte(arg.Data)!=null)
                {
                    ReceiveDataEvent.Invoke(null, arg);
                }
                
                return EasyTask.CompletedTask;
            };
        }
        public static TcpHelper Instance { get { return instance; } }
        private TcpService service = new TcpService();

        public int Host;

       /* public TcpHelper()
        {
            if (service == null)
            {
                service = new TcpService();
                service.Connecting = (client, e) => {
                    Console.WriteLine("有客户端正在连接");
                    return EasyTask.CompletedTask; 
                };//有客户端正在连接
                service.Connected = (client, e) => {
                    Console.WriteLine("客户端成功连接");
                    return EasyTask.CompletedTask; 
                };//有客户端连接
                service.Disconnected = (client, e) => {
                    Console.WriteLine("客户端断开连接");
                    return EasyTask.CompletedTask; 
                };//有客户端断开连接
            }
        }*/
       public void SetTCP()
        {
            //service.Received =
        }
        
       
        public void OpenTCP(int listenAddress)
        {
            Host=listenAddress;
            service.Start(listenAddress);
        }

        public void CloseTCP()
        {
            service?.Stop();
        }

        public override string ToString()
        {
            string msg = "";
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    msg = ip.ToString();
                }
            }
            return msg;
        }
        public string GetIPAddress()
        {
            string ip = "0.0.0.0";
            try
            {
                WebClient MyWebClient = new WebClient();
                MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

                Byte[] pageData = MyWebClient.DownloadData("http://www.net.cn/static/customercare/yourip.asp"); //从指定网站下载数据

                string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312，则使用这句

                int i1 = pageHtml.IndexOf("<h2>"), i2 = pageHtml.IndexOf("</h2>");
                ip = pageHtml.Substring(i1 + 1, i2 - 1 - i1);
                ip = ip.Substring(3, ip.Length - 3);
            }
            catch (WebException webEx)
            {
                webEx.Message.ToString();
            }
            return ip;
        }
    }
    /// <summary>
    /// 串口接收数据事件的参数
    /// </summary>

}
